package com.baizhi.action;

import com.baizhi.entity.Auction;
import com.baizhi.service.AuctionService;
import com.baizhi.service.impl.AuctionServiceImpl;
import com.baizhi.util.Cahngliang;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

public class AuctionAction {
    private AuctionService as = new AuctionServiceImpl();
    private int pageNum = 1;
    private int newPageNum;
    private int totalPage;
    private List<Auction> auctions;
    private String auctionName;
    private String auctionDesc;
    private String auctionStartTime;
    private String auctionEndTime;
    private double auctionUpset;
    private int deleteId;
    private int selectId;
    private int selectToBidId;
    private Auction selectAuction;
    private Auction updateAuction;
    private Auction insertAuction;
    private Auction selectToBidAuction;
    private File up;
    private String upFileName;




    public String show(){
        HttpServletRequest request = ServletActionContext.getRequest();
        String mark = (String)request.getAttribute("mark");
        if(mark!=null&&mark.equals("selectFuzzy")){
            auctions = (List<Auction>)request.getAttribute("auctions");
        }else{
            int pageSize = Cahngliang.getPageSize();
            System.err.println("pageNum"+pageNum);
            auctions = as.selectByPage(pageNum, pageSize);
            int count = as.count();
            totalPage = count%pageSize==0?count/pageSize:(count/pageSize)+1;

        }
        return "show";
    }

    public String selectFuzzy(){
        System.out.println("1"+auctionName);
        System.out.println("2"+auctionDesc);
        System.out.println("3"+auctionStartTime);
        System.out.println("4"+auctionEndTime);
        System.out.println("5"+auctionUpset);

        auctions = as.selectFuzzy(auctionName, auctionDesc, auctionStartTime, auctionEndTime, auctionUpset);
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("mark","selectFuzzy");
        request.setAttribute("auctions",auctions);
        for (Auction a:auctions) {
            System.err.println(a);
        }
        return "selectFuzzy";
    }

    public String deleteById(){
        as.deleteById(deleteId);
        return "deleteById";
    }

    public String selectById(){
        System.err.println("1"+selectId);
        selectAuction = as.selectById(selectId);
        System.err.println(selectAuction);
        return "selectById";
    }

    public String upLoad(){
        String uuid = UUID.randomUUID().toString();
        try {
            FileInputStream fis = new FileInputStream(up);
            FileOutputStream fos = new FileOutputStream("C://Users//94379//IdeaProjects//Bidding-parent//Bidding-web//src//main//webapp//images//"+uuid+upFileName);
            while(true){
                int n = fis.read();
                if(n==-1) break;
                fos.write(n);
                fos.close();
                fis.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "upLoad";
    }

    public String updateById(){
        as.updateById(updateAuction);
        return "updateById";
    }

    public String insert(){
        as.insert(insertAuction);
        return "insert";
    }

    public String selectToBid(){
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute("auctionId",selectToBidId);
        selectToBidAuction = as.selectById(selectToBidId);
        return "selectToBid";
    }



    public List<Auction> getAuctions() {
        return auctions;
    }

    public void setAuctions(List<Auction> auctions) {
        this.auctions = auctions;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public String getAuctionName() {
        return auctionName;
    }

    public void setAuctionName(String auctionName) {
        this.auctionName = auctionName;
    }

    public String getAuctionDesc() {
        return auctionDesc;
    }

    public void setAuctionDesc(String auctionDesc) {
        this.auctionDesc = auctionDesc;
    }

    public String getAuctionStartTime() {
        return auctionStartTime;
    }

    public void setAuctionStartTime(String auctionStartTime) {
        this.auctionStartTime = auctionStartTime;
    }

    public String getAuctionEndTime() {
        return auctionEndTime;
    }

    public void setAuctionEndTime(String auctionEndTime) {
        this.auctionEndTime = auctionEndTime;
    }

    public double getAuctionUpset() {
        return auctionUpset;
    }

    public void setAuctionUpset(double auctionUpset) {
        this.auctionUpset = auctionUpset;
    }


    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getNewPageNum() {
        return newPageNum;
    }

    public void setNewPageNum(int newPageNum) {
        this.newPageNum = newPageNum;
    }

    public int getDeleteId() {
        return deleteId;
    }

    public void setDeleteId(int deleteId) {
        this.deleteId = deleteId;
    }

    public int getSelectId() {
        return selectId;
    }

    public void setSelectId(int selectId) {
        this.selectId = selectId;
    }

    public Auction getSelectAuction() {
        return selectAuction;
    }

    public void setSelectAuction(Auction selectAuction) {
        this.selectAuction = selectAuction;
    }

    public File getUp() {
        return up;
    }

    public void setUp(File up) {
        this.up = up;
    }

    public String getUpFileName() {
        return upFileName;
    }

    public void setUpFileName(String upFileName) {
        this.upFileName = upFileName;
    }

    public Auction getUpdateAuction() {
        return updateAuction;
    }

    public void setUpdateAuction(Auction updateAuction) {
        this.updateAuction = updateAuction;
    }

    public Auction getInsertAuction() {
        return insertAuction;
    }

    public void setInsertAuction(Auction insertAuction) {
        this.insertAuction = insertAuction;
    }

    public int getSelectToBidId() {
        return selectToBidId;
    }

    public void setSelectToBidId(int selectToBidId) {
        this.selectToBidId = selectToBidId;
    }

    public Auction getSelectToBidAuction() {
        return selectToBidAuction;
    }

    public void setSelectToBidAuction(Auction selectToBidAuction) {
        this.selectToBidAuction = selectToBidAuction;
    }
}
